package br.ufrpe.bsi.pvs.database.roupa;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import br.ufrpe.bsi.pvs.database.Conexao;
import br.ufrpe.bsi.pvs.model.roupa.Estilo;

public class EstiloDAOImpl implements EstiloDAO {

	@Override
	public void salvar(Estilo object) {

	}

	@Override
	public void remover(Estilo object) {

	}

	@Override
	public void remover(Serializable id) {

	}

	@Override
	public List<Estilo> listar() {
		List<Estilo> estilos = new ArrayList<Estilo>();

		try (Connection conn = Conexao.getConnection()) {
			PreparedStatement ps = conn
					.prepareStatement("select * from estilo");
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				estilos.add(Estilo.getEstilo(rs.getString("nome")));
			}

			ps.close();

		} catch (Exception e) {
			System.out.println("[DB ERROR] cant list Estilos");
		}

		return estilos;
	}

	@Override
	public Estilo pesquisar(Serializable id) {
		Estilo e = null;
		try (Connection conn = Conexao.getConnection()) {
			PreparedStatement ps = conn
					.prepareStatement("select nome from estilo where id = ?");
			ps.setInt(1, (Integer) id);

			ResultSet rs = ps.executeQuery();

			while (rs.next())
				e = Estilo.getEstilo(rs.getString("nome"));

			ps.close();
		} catch (Exception ex) {
			System.out.println("[DB ERROR] cant found estilo id: " + id);
		}

		return e;
	}

	@Override
	public Estilo pesquisar(String name) {
		Estilo e = null;
		try (Connection conn = Conexao.getConnection()) {
			PreparedStatement ps = conn
					.prepareStatement("select nome from estilo where nome = ?");
			ps.setString(1, name);

			ResultSet rs = ps.executeQuery();

			while (rs.next())
				e = Estilo.getEstilo(rs.getString("nome"));

			ps.close();
		} catch (Exception ex) {
			System.out.println("[DB ERROR] cant found estilo named: " + name);
		}

		return e;
	}

}
